package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Bean.User;

import org.litepal.LitePal;

public class Wode extends AppCompatActivity {
    private LinearLayout more_info, Lv_nicheng, Lv_username, Lv_lm, lv_qrcode;
    private TextView tv_nicheng, tv_username, tv_lm;
    private int uid;
    private ImageView userimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wode);


        initView();
        setListener();
    }

    private void setListener() {
//更多信息界面
        more_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Wode.this, MoreInformation.class);
                startActivity(intent);
            }
        });

        //用户名
        Lv_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                所在的activity
                AlertDialog.Builder builder = new AlertDialog.Builder(Wode.this);
                final View v = LayoutInflater.from(Wode.this).inflate(R.layout.dialog_customize, null);
                builder.setIcon(R.mipmap.bi);
                builder.setTitle("更改用户名");
//                创建视图
                builder.setView(v);
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 获取EditView中的输入内容
                        EditText edit_dialog = v.findViewById(R.id.edit_dialog);
                        User u = LitePal.find(User.class, uid);
                        u.setName(edit_dialog.getText().toString());
                        if (u.update(uid) == 1) {
                            //    把 获取EditView中的输入内容 传入到TextView中
                            tv_username.setText(edit_dialog.getText().toString());
                            SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                            SharedPreferences.Editor editor = sp.edit();
                            editor.putString("myname", edit_dialog.getText().toString());

                            Toast.makeText(Wode.this, "修改成功", Toast.LENGTH_SHORT).show();
                            editor.apply();
                        } else {
                            Toast.makeText(Wode.this, "修改失败", Toast.LENGTH_SHORT).show();
                        }
//                        把 获取EditView中的输入内容 传入到TextView中
//                        tv_username.setText(edit_dialog.getText().toString());

                    }
                });

                builder.create().show();
            }
        });

//       昵称
        Lv_nicheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                所在的activity
                AlertDialog.Builder builder1 = new AlertDialog.Builder(Wode.this);
                final View v = LayoutInflater.from(Wode.this).inflate(R.layout.dialog_customize, null);
                builder1.setIcon(R.mipmap.bi);
                builder1.setTitle("更改昵称");
//                创建视图
                builder1.setView(v);
                builder1.setNegativeButton("取消", null);
                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 获取EditView中的输入内容
                        EditText edit_dialog = v.findViewById(R.id.edit_dialog);
                        User u = LitePal.find(User.class, uid);
//                        把 获取EditView中的输入内容 传入到TextView中
                        tv_nicheng.setText(edit_dialog.getText().toString());
                        SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("mynickname", edit_dialog.getText().toString());
                        editor.apply();
                    }
                });

                builder1.create().show();
            }
        });


        //留言
        Lv_lm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                所在的activity
                AlertDialog.Builder builder3 = new AlertDialog.Builder(Wode.this);
                final View v = LayoutInflater.from(Wode.this).inflate(R.layout.dialog_customize, null);
                builder3.setIcon(R.mipmap.bi);
                builder3.setTitle("更改留言");
//                创建视图
                builder3.setView(v);
                builder3.setNegativeButton("取消", null);
                builder3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // 获取EditView中的输入内容
                        EditText edit_dialog = v.findViewById(R.id.edit_dialog);
//                        把 获取EditView中的输入内容 传入到TextView中
                        tv_lm.setText(edit_dialog.getText().toString());
                        SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("mysign", edit_dialog.getText().toString());
                        editor.apply();

                    }
                });

                builder3.create().show();
            }
        });
//        跳转到二维码页面
        lv_qrcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(Wode.this, Qr_code.class);
                startActivity(intent1);

            }
        });
    }

    private void initView() {
        more_info = findViewById(R.id.more_info);
        Lv_nicheng = findViewById(R.id.Lv_nicheng);
        tv_nicheng = findViewById(R.id.tv_nicheng);
        Lv_username = findViewById(R.id.Lv_username);
        tv_username = findViewById(R.id.tv_username);
        Lv_lm = findViewById(R.id.Lv_lm);
        tv_lm = findViewById(R.id.tv_lm);
        lv_qrcode = findViewById(R.id.lv_qrcode);
        userimg = findViewById(R.id.userimg);
//        同步头像用户名
        SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);

        uid = sp.getInt("myid", 0);

        User u = LitePal.find(User.class, uid);
//头像
        userimg.setImageResource(sp.getInt("myimg", -1));
//用户名
        tv_username.setText(sp.getString("myname", ""));
//昵称
        tv_nicheng.setText(u.getNickName());
        tv_nicheng.setText(sp.getString("mynickname", ""));

//        留言
        tv_lm.setText(sp.getString("mysign", ""));
    }
}